package com.wkn.wknAdmin.service.impl;

import com.wkn.wknAdmin.entity.Role;
import com.wkn.wknAdmin.entity.User;
import com.wkn.wknAdmin.mapper.RoleMapper;
import com.wkn.wknAdmin.result.ResultVO;
import com.wkn.wknAdmin.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Title:
 * @Description:
 * @Author: WKN
 * CreateDate: 2022/8/13 11:22
 */
@Service
public class RoleServiceImpl implements RoleService {

    ResultVO result = new ResultVO();


    @Resource
    private RoleMapper roleMapper;

    @Override
    public ResultVO save(Role role) {
        String name = role.getRname();
        Role getRole = roleMapper.getOne(name);
        if (role.getId() == null) {
            if (getRole != null) {
                return result.Fail("角色名已存在");
            }
            int insert = roleMapper.insert(role);
            return insert > 0 ? result.Success("添加成功") : result.Fail("添加失败");
        }
        int update = roleMapper.update(role);
        return update > 0 ? result.Success("修改成功") : result.Fail("修改失败");
    }



    @Override
    public Role getOne(String rname) {
        return roleMapper.getOne(rname);
    }



    /**
     * 分页查询用户
     *
     * @param pageNum  当前页码
     * @param pageSize 每一页展示数据的条数
     * @return
     */
    @Override
    public List<Role> selectPage(Integer pageNum, Integer pageSize, String rname) {
        List<Role> roles = roleMapper.selectPage(pageNum, pageSize, rname);
        return roles;
    }

    @Override
    public int getTotalCount(String rname) {
        return roleMapper.getTotalCount(rname);
    }


    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }
}
